package com.quan;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.scheduling.annotation.EnableAsync;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;

import tk.mybatis.spring.annotation.MapperScan;

@EnableCaching
@MapperScan(basePackages = "com.quan.**.mapper")
@EnableAsync
@SpringBootApplication
public class ApplicationMars implements CommandLineRunner, DisposableBean {

	private static final Logger logger = LoggerFactory.getLogger(ApplicationMars.class);
	
	public static void main(String[] args) {
		SpringApplication.run(ApplicationMars.class, args);
	}

	@Override
	public void run(String... args) throws Exception {
		logger.info("**********************************************");
		logger.info("服务器启动.....");
		logger.info("**********************************************");
	}

	@Override
	public void destroy() throws Exception {
		logger.info("**********************************************");
		logger.info("服务器关闭.....");
		logger.info("**********************************************");
	}

	
	  @Bean public HttpMessageConverters fastJsonHttpMessageConverters() { 
		  //1.需要定义一个convert转换消息的对象; 
		  FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter(); 
	  //2:添加fastJson的配置信息; 
		  FastJsonConfig fastJsonConfig = new FastJsonConfig();
	  fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
	  
	  // 3.将所有Long类型转换成字符串类型（不转换的话，传到前端Long类型数据会丢失精度，ID字段类型为Long）
	  fastJsonConfig.setSerializerFeatures(SerializerFeature.BrowserCompatible);
	  
	  // 4.处理中文乱码问题 
	  List<MediaType> fastMediaTypes = new ArrayList<>();
	  fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
	  
	  // 5.在convert中添加配置信息.
	  fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);
	  fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
	  HttpMessageConverter<?> converter = fastJsonHttpMessageConverter; return new
	  HttpMessageConverters(converter); }
	 

}
